package com.isoft.airportsystem.listener;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.JacksonXmlModule;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.isoft.airportsystem.config.SubRunnerContext;
import com.isoft.airportsystem.entity.AFID.MSG_AFID;
import com.isoft.airportsystem.entity.AIRL.MSG_AIRL;
import com.isoft.airportsystem.entity.AOPT.MSG_AOPT;
import com.isoft.airportsystem.entity.ARRE.MSG_ARRE;
import com.isoft.airportsystem.entity.BLLS.MSG_BLLS;
import com.isoft.airportsystem.entity.BORE.MSG_BORE;
import com.isoft.airportsystem.entity.CANE.MSG_CANE;
import com.isoft.airportsystem.entity.CFCE.MSG_CFCE;
import com.isoft.airportsystem.entity.CFIE.MSG_CFIE;
import com.isoft.airportsystem.entity.CFUE.MSG_CFUE;
import com.isoft.airportsystem.entity.CKIE.MSG_CKIE;
import com.isoft.airportsystem.entity.CKLS.MSG_CKLS;
import com.isoft.airportsystem.entity.CKOE.MSG_CKOE;
import com.isoft.airportsystem.entity.DEPE.MSG_DEPE;
import com.isoft.airportsystem.entity.DFDE.MSG_DFDE;
import com.isoft.airportsystem.entity.DFDL.MSG23;
import com.isoft.airportsystem.entity.DLYE.MSG_DLYE;
import com.isoft.airportsystem.entity.FETT.MSG_FETT;
import com.isoft.airportsystem.entity.FPTT.MSG_FPTT;
import com.isoft.airportsystem.entity.FRTT.MSG_FRTT;
import com.isoft.airportsystem.entity.GTLS.MSG_GTLS;
import com.isoft.airportsystem.entity.HBTT.MSG_HBTT;
import com.isoft.airportsystem.entity.LBDE.MSG_LBDE;
import com.isoft.airportsystem.entity.ONER.MSG_ONRE;
import com.isoft.airportsystem.entity.POKE.MSG_POKE;
import com.isoft.airportsystem.entity.STLS.MSG_STLS;
import com.isoft.airportsystem.service.*;
import org.apache.commons.io.monitor.FileAlterationListener;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.UUID;


@Component
public class FileListener implements FileAlterationListener { //文件监听器
    public static ObjectMapper xmlMapper = new XmlMapper(); //xml转换为json
    // 手动声明日志记录器
    private static final Logger log = LoggerFactory.getLogger(FileListener.class);
    @Value("${file.monitor.path}")
    private String filePath; // 从配置文件中读取文件监视器的路径

    @Override
    public void onStart(FileAlterationObserver fileAlterationObserver) {
        log.info("文件监听开始.....");
    }

    @Override
    public void onDirectoryCreate(File file) {
        log.info("文件夹创建: {}", file.getName());
    }

    @Override
    public void onDirectoryChange(File file) {
        log.info("文件夹修改: {}", file.getName());
    }

    @Override
    public void onDirectoryDelete(File file) {
        log.info("文件夹删除: {}", file.getName());
    }

    @Override
    public void onFileCreate(File file) {//文件创建
        try {
            String fileName = file.getName(); //获取文件名
            log.info("文件创建: {}", fileName); //使用日志记录文件名

            // 安全地解析文件名
            String subName;
            int index = fileName.indexOf('2');
            if (index > 0) {
                subName = fileName.substring(0, index - 1); //截取文件名
                log.info("提取文件名：{}", subName); //使用日志记录截取的文件名

                // 使用try-with-resources语句自动关闭文件输入流
                try (FileInputStream input = new FileInputStream(new File(filePath, fileName))) {
                    JacksonXmlModule module = new JacksonXmlModule(); //创建xml模块
                    XmlMapper mapper = new XmlMapper(module); //创建xmlMapper对象

                    // 生成metaid
                    String metaid = UUID.randomUUID().toString().replace("-", "").toLowerCase();

                    switch (subName) { //判断文件名
                        case "BASE-APUE" -> { //如果文件名为BASE-APUE
                            MSG_AOPT dataAopt = mapper.readValue(input, MSG_AOPT.class); //将xml文件解析为MSG_AOPT对象
                            log.debug("解析数据: {}", dataAopt); //使用日志记录解析的对象
//                            if (dataAopt.getApotData() != null) { //判断解析的对象是否为空
//                                SubRunnerContext.getBean(ApueService.class).add(dataAopt.getApotData(), dataAopt.getMeta());//将解析的对象上传到达梦数据库
//                            }
                            if (dataAopt.getApotData() != null) { //判断解析的对象是否为空
                                SubRunnerContext.getBean(com.isoft.airportsystem.service.ApueService.class).add(dataAopt.getApotData(), dataAopt.getMeta());//将解析的对象上传到达梦数据库
                            }
                        }
                        // 其他case保持不变
                        case "DFME-AFID" -> {
                            MSG_AFID dataAfid = mapper.readValue(input, MSG_AFID.class);
                            log.debug("解析数据: {}", dataAfid);
                            if (dataAfid.getDflt_afidData() != null) {
                                SubRunnerContext.getBean(com.isoft.airportsystem.service.AFIDService.class).add(dataAfid.getDflt_afidData(), dataAfid.getMeta());
                            }
                        }
                        case "DFME-ARRE" -> {
                            MSG_ARRE dataArre = mapper.readValue(input, MSG_ARRE.class);
                            log.debug("解析数据: {}", dataArre);
                            if (dataArre.getDflt_arreData() != null) {
                                SubRunnerContext.getBean(com.isoft.airportsystem.service.ArreService.class).add(dataArre.getDflt_arreData(), dataArre.getMeta());
                            }
                        }
                        case "BASE-CFIE" -> {
                            MSG_CFIE dataCfie = mapper.readValue(input, MSG_CFIE.class);
                            log.debug("解析数据: {}", dataCfie);
                            if (dataCfie.getCrft_cfieData() != null) {
                                SubRunnerContext.getBean(com.isoft.airportsystem.service.CFIEService.class).add(dataCfie.getCrft_cfieData(), dataCfie.getMeta());
                            }
                        }
                        case "BASE-CFUE" -> {
                            MSG_CFUE dataCfue = mapper.readValue(input, MSG_CFUE.class);
                            log.debug("解析数据: {}", dataCfue);
                            if (dataCfue.getCrft_cfueData() != null) {
                                SubRunnerContext.getBean(com.isoft.airportsystem.service.CFUEService.class).add(dataCfue.getCrft_cfueData(), dataCfue.getMeta());
                            }
                        }
                        case "DFOE-DFDE" -> {
                            MSG_DFDE dataDfde = mapper.readValue(input, MSG_DFDE.class);
                            log.debug("解析数据: {}", dataDfde);
                            if (dataDfde.getDflt_dfdeData() != null) {
                                SubRunnerContext.getBean(com.isoft.airportsystem.service.DFDEService.class).add(dataDfde.getDflt_dfdeData(), dataDfde.getMeta());
                            }
                        }
                        case "DFME-AIRL" -> {
                            MSG_AIRL dataAirl = mapper.readValue(input, MSG_AIRL.class);
                            log.debug("解析数据: {}", dataAirl);
                            if (dataAirl.getDflt_airlData() != null) {
                                SubRunnerContext.getBean(com.isoft.airportsystem.service.AIRLService.class).add(dataAirl.getDflt_airlData(), dataAirl.getMeta());
                            }
                        }
                        case "DFME-BLLS" -> {
                            MSG_BLLS dataBlls = mapper.readValue(input, MSG_BLLS.class);
                            log.debug("解析数据: {}", dataBlls);
                            if (dataBlls.getDflt_bllsData() != null) {
                                SubRunnerContext.getBean(com.isoft.airportsystem.service.BllsService.class).add(dataBlls.getDflt_bllsData(), dataBlls.getMeta());
                            }
                        }
                        case "DFME-BORE" -> {
                            MSG_BORE dataBore = mapper.readValue(input, MSG_BORE.class);
                            log.debug("解析数据: {}", dataBore);
                            if (dataBore.getDflt_boreData() != null) {
                                SubRunnerContext.getBean(com.isoft.airportsystem.service.BoreService.class).add(dataBore.getDflt_boreData(), dataBore.getMeta());
                            }
                        }
                        case "DFME-CANE" -> {
                            MSG_CANE dataCane = mapper.readValue(input, MSG_CANE.class);
                            log.debug("解析数据: {}", dataCane);
                            if (dataCane.getDflt_caneData() != null) {
                                SubRunnerContext.getBean(com.isoft.airportsystem.service.CaneService.class).add(dataCane.getDflt_caneData(), dataCane.getMeta());
                            }
                        }
                        case "DFME-CFCE" -> {
                            MSG_CFCE dataCfce = mapper.readValue(input, MSG_CFCE.class);
                            log.debug("解析数据: {}", dataCfce);
                            if (dataCfce.getDflt_cfceData() != null) {
                                SubRunnerContext.getBean(com.isoft.airportsystem.service.CfceService.class).add(dataCfce.getDflt_cfceData(), dataCfce.getMeta());
                            }
                        }
                        case "DFME-CKIE" -> {
                            MSG_CKIE dataCkie = mapper.readValue(input, MSG_CKIE.class);
                            log.debug("解析数据: {}", dataCkie);
                            if (dataCkie.getDflt_ckieData() != null) {
                                SubRunnerContext.getBean(com.isoft.airportsystem.service.CkieService.class).add(dataCkie.getDflt_ckieData(), dataCkie.getMeta());
                            }
                        }
                        case "DFME-CKLS" -> {
                            MSG_CKLS dataCkls = mapper.readValue(input, MSG_CKLS.class);
                            log.debug("解析数据: {}", dataCkls);
                            if (dataCkls.getDfltCklsData() != null) {
                                SubRunnerContext.getBean(com.isoft.airportsystem.service.CKLSService.class).add(dataCkls.getDfltCklsData(), dataCkls.getMeta());
                            }
                        }
                        case "DFME-CKOE" -> {
                            MSG_CKOE dataCkoe = mapper.readValue(input, MSG_CKOE.class);
                            log.debug("解析数据: {}", dataCkoe);
                            if (dataCkoe.getDflt_ckoeData() != null) {
                                SubRunnerContext.getBean(com.isoft.airportsystem.service.CkoeService.class).add(dataCkoe.getDflt_ckoeData(), dataCkoe.getMeta());
                            }
                        }
                        case "DFME-DEPE" -> {
                            MSG_DEPE dataDepe = mapper.readValue(input, MSG_DEPE.class);
                            log.debug("解析数据: {}", dataDepe);
                            if (dataDepe.getDflt_depeData() != null) {
                                SubRunnerContext.getBean(DepeService.class).add(dataDepe.getDflt_depeData(), dataDepe.getMeta());
                            }
                        }
                        case "DFME-DLYE" -> {
                            MSG_DLYE dataDlye = mapper.readValue(input, MSG_DLYE.class);
                            log.debug("解析数据: {}", dataDlye);
                            if (dataDlye.getDflt_dlyeData() != null) {
                                SubRunnerContext.getBean(DlyeService.class).add(dataDlye.getDflt_dlyeData(), dataDlye.getMeta());
                            }
                        }
                        case "DFME-FETT" -> {
                            MSG_FETT dataFett = mapper.readValue(input, MSG_FETT.class);
                            log.debug("解析数据: {}", dataFett);
                            if (dataFett.getDflt_fettData() != null) {
                                SubRunnerContext.getBean(FettService.class).add(dataFett.getDflt_fettData(), dataFett.getMeta());
                            }
                        }
                        case "DFME-FPTT" -> {
                            MSG_FPTT dataFptt = mapper.readValue(input, MSG_FPTT.class);
                            log.debug("解析数据: {}", dataFptt);
                            if (dataFptt.getDflt_fpttData() != null) {
                                SubRunnerContext.getBean(FPTTService.class).add(dataFptt.getDflt_fpttData(), dataFptt.getMeta());
                            }
                        }
                        case "DFME-FRTT" -> {
                            MSG_FRTT dataFrtt = mapper.readValue(input, MSG_FRTT.class);
                            log.debug("解析数据: {}", dataFrtt);
                            if (dataFrtt.getDflt_frttData() != null) {
                                SubRunnerContext.getBean(FrttService.class).add(dataFrtt.getDflt_frttData(), dataFrtt.getMeta());
                            }
                        }
                        case "DFME-GTLS" -> {
                            MSG_GTLS dataGtls = mapper.readValue(input, MSG_GTLS.class);
                            log.debug("解析数据: {}", dataGtls);
                            if (dataGtls.getDflt_gtls() != null) {
                                SubRunnerContext.getBean(GtlsService.class).add(dataGtls.getDflt_gtls(), dataGtls.getMeta());
                            }
                        }
                        case "DFME-HBTT" -> {
                            MSG_HBTT dataHbtt = mapper.readValue(input, MSG_HBTT.class);
                            log.debug("解析数据: {}", dataHbtt);
                            if (dataHbtt.getDflt_hbttData() != null) {
                                SubRunnerContext.getBean(HBTTService.class).add(dataHbtt.getDflt_hbttData(), dataHbtt.getMeta());
                            }
                        }
                        case "DFME-LBDE" -> {
                            MSG_LBDE dataLbde = mapper.readValue(input, MSG_LBDE.class);
                            log.debug("解析数据: {}", dataLbde);
                            if (dataLbde.getDflt_lbdeData() != null) {
                                SubRunnerContext.getBean(LBDEService.class).add(dataLbde.getDflt_lbdeData(), dataLbde.getMeta());
                            }
                        }
                        case "DFME-ONRE" -> {
                            MSG_ONRE dataOnre = mapper.readValue(input, MSG_ONRE.class);
                            log.debug("解析数据: {}", dataOnre);
                            if (dataOnre.getDflt_onreData() != null) {
                                SubRunnerContext.getBean(OnreService.class).add(dataOnre.getDflt_onreData(), dataOnre.getMeta());
                            }
                        }
                        case "DFME-POKE" -> {
                            MSG_POKE dataPoke = mapper.readValue(input, MSG_POKE.class);
                            log.debug("解析数据: {}", dataPoke);
                            if (dataPoke.getDflt_pokeData() != null) {
                                SubRunnerContext.getBean(PokeService.class).add(dataPoke.getDflt_pokeData(), dataPoke.getMeta());
                            }
                        }
                        case "DFME-STLS" -> {
                            MSG_STLS dataStls = mapper.readValue(input, MSG_STLS.class);
                            log.debug("解析数据: {}", dataStls);
                            if (dataStls.getDflt_stlsData() != null) {
                                SubRunnerContext.getBean(STLSService.class).add(dataStls.getDflt_stlsData(), dataStls.getMeta());
                            }
                        }
                        case "DFOE-DFDL" -> { //DFDL
                            MSG23 dataDfdl = mapper.readValue(input, MSG23.class); //MSG23
                            log.debug("解析数据: {}", dataDfdl); //MSG23
                            SubRunnerContext.getBean(DfdlService.class).insert(dataDfdl); //MSG23
                        }
                        case "DFOE-DFIE" -> {
                            MSG23 dataDfie = mapper.readValue(input, MSG23.class);
                            log.debug("解析数据: {}", dataDfie);
                            SubRunnerContext.getBean(DfdlService.class).insert(dataDfie);
                        }
                        default -> {
                        }
                    }
                } catch (IOException e) {
                    log.error("解析xml文件失败: {}", e.getMessage(), e);
                }
            } else {
                log.warn("文件名格式不正确，无法解析: {}", fileName);
            }
        } catch (Exception e) {
            log.error("处理文件创建事件失败: {}", e.getMessage(), e);
        }
    }

    @Override
    public void onFileChange(File file) {
        log.info("文件修改: {}", file.getName());
    }

    @Override
    public void onFileDelete(File file) {
        log.info("文件删除: {}", file.getName());
    }

    @Override
    public void onStop(FileAlterationObserver fileAlterationObserver) {
        log.info("文件监听结束....");
    }
}